package com.example.OneToOne.controller;
import com.example.OneToOne.entity.Author;
import com.example.OneToOne.entity.Book;
import com.example.OneToOne.service.AuthorAndBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OneToOneController {
    @Autowired
    private AuthorAndBookService authorAndBookService;
    @RequestMapping("/save")
    public String save(){
        authorAndBookService.saveAll();
        return "作者和书名保存成功";
    }
    @RequestMapping("/findAllAuthor")
    public List<Author> findAllAuthor(){
        return authorAndBookService.findAllAuthor();
    }
    @RequestMapping("/findAllBook")
    public List<Book> findAllBook(){
        return authorAndBookService.findAllBook();
    }
    @RequestMapping("/findByAuthorId/{id}")
    public Book findByAuthorId(@PathVariable int id){
        return authorAndBookService.findByAuthor_id(id);
    }
    @RequestMapping("/findByBookId/{id}")
    public Author findByBookId(@PathVariable int id){
        return authorAndBookService.findByBook_id(id);
    }
    @RequestMapping("/getOneBook/{id}")
    public Book getOneBook(@PathVariable int id){
        return authorAndBookService.getOneBook(id);
    }
    @RequestMapping("/getOneAuthor/{id}")
    public Author getOneAuthor(@PathVariable int id){
        return authorAndBookService.getOneAuthor(id);
    }
    @DeleteMapping("/del/{id}")
    public String delByAuthorById(@PathVariable int id){ return  authorAndBookService.delByAuthorId(id);}
    @PutMapping("/update/{id}")
    public Author updateAuthorById(@PathVariable int id,@RequestBody Author author){return authorAndBookService.updateAuthorById(id,author);}
}
